/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.TypeConverters.BuiltIn
{
	internal sealed class ReferenceTargetPropertyDescriptor : PropertyDescriptor
	{
		#region Public Properties
		public override Type ComponentType
		{
			get
			{
				return context.PropertyDescriptor.ComponentType;
			}
		}

		public override bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public override Type PropertyType
		{
			get
			{
				return typeof(ReferenceLinkType);
			}
		}

		public override string DisplayName
		{
			get
			{
				return displayName;
			}
		}

		public override string Description
		{
			get
			{
				return string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Resources.Text.ResolveReferencesComponentDescription, target.FullPath, Environment.NewLine, target.LinkType);
			}
		}
		#endregion

		#region Private / Protected
		private ReferenceLinkType defaultLinkType;
		private readonly string displayName;
		private readonly ReferenceTarget target;
		private readonly ITypeDescriptorContext context;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ReferenceTargetPropertyDescriptor" /> class.
		/// </summary>
		public ReferenceTargetPropertyDescriptor(string name, ReferenceTarget target, ITypeDescriptorContext context)
			: base(name, new Attribute[] { new NotifyParentPropertyAttribute(true) })
		{
			this.target = target;
			this.context = context;

			defaultLinkType = target.LinkType;

			displayName = CreateDisplayName(target);
		}
		#endregion

		#region Methods
		private static string CreateDisplayName(ReferenceTarget target)
		{
			Uri uri;
			if (Uri.TryCreate(Environment.ExpandEnvironmentVariables(target.FullPath).Replace(@"\\", @"\"), UriKind.RelativeOrAbsolute, out uri))
			{
				if (uri.IsAbsoluteUri)
				{
					Uri dataUri;
					if (Uri.TryCreate(SandcastleBuildEngineProvider.Paths.ReflectionDataRoot.Replace(@"\\", @"\"), UriKind.RelativeOrAbsolute, out dataUri))
					{
						if (dataUri.IsBaseOf(uri))
							return Resources.Text.FrameworkLinkType;
					}
				}

				if (string.Equals(@".\reflection.xml", uri.ToString(), StringComparison.OrdinalIgnoreCase))
					return Resources.Text.ProjectLinkType;

				return (uri.IsAbsoluteUri) ? uri.LocalPath : System.Web.HttpUtility.UrlDecode(uri.ToString());
			}

			return target.FullPath;
		}

		public override bool ShouldSerializeValue(object component)
		{
			return target.LinkType != defaultLinkType;
		}

		public override bool CanResetValue(object component)
		{
			return true;
		}

		public override void ResetValue(object component)
		{
			target.LinkType = defaultLinkType;
		}

		public override object GetValue(object component)
		{
			return target.LinkType;
		}

		public override void SetValue(object component, object value)
		{
			target.LinkType = (ReferenceLinkType) value;
		}
		#endregion
	}
}
